package com.nullpointer.xchatfinal.utils;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.nullpointer.xchatfinal.object.BlockUser;
import com.nullpointer.xchatfinal.object.ChattedUser;
import com.nullpointer.xchatfinal.object.TargetChat;
import com.nullpointer.xchatfinal.object.User;
import com.nullpointer.xchatfinal.object.UserInfo.GenderType;

public class DataManager {
	private static DataManager mDataManager;
	private SharedPreferences pref;
	private Editor editor;
	private static final String DEVICE_ID = "device_id";
	private static final String CURRENT_TARGET_CHAT = "CURRENT_TARGET_CHAT";

	private static final String CURRENT_USER = "CURRENT_USER";

	private static final String IS_LOGOUT = "is_logout";

	private static final String IS_SHOW_POST = "IS_SHOW_POST";
	private static final String IS_APP_PAUSE = "IS_APP_PAUSE";

	// SETTING
	private static final String NOTIFICATION_SETTING = "NOTIFICATION_SETTING";
	private static final String SOUND_SETTING = "SOUND_SETTING";
	private static final String VIBRATION_SETTING = "VIBRATION_SETTING";
	private static final String INVISIBLE_SETTING = "INVISIBLE_SETTING";

	// FIND FRIEND SETTING
	private static final String AGE_MIN = "AGE_MIN";
	private static final String AGE_MAX = "AGE_MAX";
	private static final String GENDER_SEARCH = "GENDER_SEARCH";

	// RATE
	private static final String IS_RATED = "IS_RATED";

	// BLOCK USER
	private static final String BLOCK_USER = "BLOCK_USER";

	private static final String LOGIN_WITH_FB = "LOGIN_WITH_FB";
	private static final String CHATTED_USER = "CHATTED_USER";

	private DataManager(Context context) {
		pref = PreferenceManager.getDefaultSharedPreferences(context);
		editor = pref.edit();
	}

	public static DataManager getInstance(Context context) {
		if (mDataManager == null) {
			mDataManager = new DataManager(context);
		}
		return mDataManager;
	}

	public void setDeviceID(String id) {
		editor.putString(DEVICE_ID, id);
		editor.commit();
	}

	public String getDeviceID() {
		return pref.getString(DEVICE_ID, null);
	}

	public void setCurrentUser(User currentUser) {
		editor.putString(CURRENT_USER, currentUser.toJson());
		editor.commit();
	}

	public User getCurrentUser() {
		String json = pref.getString(CURRENT_USER, null);
		if (json == null) {
			return null;
		}
		return User.fromJsonToUser(json);
	}

	public boolean isShowPost() {
		return pref.getBoolean(IS_SHOW_POST, false);
	}

	public void setIsShowPost(boolean isShowPost) {
		editor.putBoolean(IS_SHOW_POST, isShowPost);
		editor.commit();
	}

	public boolean isLoginWithFB() {
		return pref.getBoolean(LOGIN_WITH_FB, false);
	}

	public void setIsLoginWithFB(boolean isLoginFB) {
		editor.putBoolean(LOGIN_WITH_FB, isLoginFB);
		editor.commit();
	}

	public boolean isLogout() {
		return pref.getBoolean(IS_LOGOUT, true);
	}

	public void setIsLogout(boolean isLogout) {
		editor.putBoolean(IS_LOGOUT, isLogout);
		editor.commit();
	}

	public boolean isAppPause() {
		return pref.getBoolean(IS_APP_PAUSE, true);
	}

	public void setIsAppPause(boolean isShowNotify) {
		editor.putBoolean(IS_APP_PAUSE, isShowNotify);
		editor.commit();
	}

	public TargetChat getCurrentTargetChat() {
		String json = pref.getString(CURRENT_TARGET_CHAT, null);
		if (json == null) {
			return null;
		}
		return TargetChat.fromJsonToTargetChat(json);
	}

	public void setCurrentTargetChat(TargetChat currentTargetChat) {
		if (currentTargetChat == null) {
			editor.putString(CURRENT_TARGET_CHAT, null);
		} else {
			editor.putString(CURRENT_TARGET_CHAT, currentTargetChat.toJson());
		}
		editor.commit();
	}

	public boolean getNotificationSetting() {
		return pref.getBoolean(NOTIFICATION_SETTING, true);
	}

	public void setNotificationSetting(boolean isOn) {
		editor.putBoolean(NOTIFICATION_SETTING, isOn);
		editor.commit();
	}

	public boolean getSoundSetting() {
		return pref.getBoolean(SOUND_SETTING, true);
	}

	public void setSoundSetting(boolean isOn) {
		editor.putBoolean(SOUND_SETTING, isOn);
		editor.commit();
	}

	public boolean getVibrationSetting() {
		return pref.getBoolean(VIBRATION_SETTING, true);
	}

	public void setVibrationSetting(boolean isOn) {
		editor.putBoolean(VIBRATION_SETTING, isOn);
		editor.commit();
	}

	public boolean getInvisibleSetting() {
		return pref.getBoolean(INVISIBLE_SETTING, false);
	}

	public void setInvisibleSetting(boolean isDontShow) {
		editor.putBoolean(INVISIBLE_SETTING, isDontShow);
		editor.commit();
	}

	public boolean isRatedApp() {
		return pref.getBoolean(IS_RATED, false);
	}

	public void setRatedApp(boolean isRatedApp) {
		editor.putBoolean(IS_RATED, isRatedApp);
		editor.commit();
	}

	public void setAgeMin(int value) {
		editor.putInt(AGE_MIN, value);
		editor.commit();
	}

	public int getAgeMin() {
		return pref.getInt(AGE_MIN, 18);
	}

	public void setAgeMax(int value) {
		editor.putInt(AGE_MAX, value);
		editor.commit();
	}

	public int getAgeMax() {
		return pref.getInt(AGE_MAX, 50);
	}

	public void setGenderSearch(GenderType genderType) {
		editor.putInt(GENDER_SEARCH, genderType.ordinal());
		editor.commit();
	}

	public GenderType getGenderSearch() {
		return GenderType.values()[pref.getInt(GENDER_SEARCH,
				GenderType.ANY.ordinal())];
	}

	public Set<String> getBlockUsers() {
		String listString = pref.getString(BLOCK_USER, null);
		if (listString != null) {
			return BlockUser.fromJsonToBlockUser(listString).getBlockUsers();
		}
		return new HashSet<String>();
	}

	public void addBlockUsers(String userID) {
		if (userID == null) {
			return;
		}
		Set<String> blockUsers = getBlockUsers();
		blockUsers.add(userID);
		BlockUser blockUser = new BlockUser();
		blockUser.setBlockUsers(blockUsers);
		editor.putString(BLOCK_USER, blockUser.toJson());
		editor.commit();
	}

	public void removeBlockUsers(String userID) {
		if (userID == null) {
			return;
		}
		Set<String> blockUsers = getBlockUsers();
		blockUsers.remove(userID);
		BlockUser blockUser = new BlockUser();
		blockUser.setBlockUsers(blockUsers);
		editor.putString(BLOCK_USER, blockUser.toJson());
		editor.commit();
	}

	public Set<Integer> getChattedUsers() {
		String listString = pref.getString(CHATTED_USER, null);
		if (listString != null) {
			return ChattedUser.fromJsonToChattedUsers(listString)
					.getChattedUsers();
		}
		return new HashSet<Integer>();
	}

	public void addChattedUsers(Integer userID) {
		if (userID == null) {
			return;
		}
		Set<Integer> chattedUsers = getChattedUsers();
		chattedUsers.add(userID);
		ChattedUser chattedUser = new ChattedUser();
		chattedUser.setChattedUsers(chattedUsers);
		editor.putString(CHATTED_USER, chattedUser.toJson());
		editor.commit();
	}

	public void removeChattedUsers(Integer userID) {
		if (userID == null) {
			return;
		}
		Set<Integer> chattedUsers = getChattedUsers();
		chattedUsers.remove(userID);
		ChattedUser chattedUser = new ChattedUser();
		chattedUser.setChattedUsers(chattedUsers);
		editor.putString(CHATTED_USER, chattedUser.toJson());
		editor.commit();
	}
}
